/* 
 * File:   main.cpp
 * Author: nata
 *
 * Created on March 13, 2012, 3:13 PM
 */

#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <algorithm>
#include <fstream>
#include <opencv/cv.h>
#include <opencv/ml.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <opencv/cvaux.h>

#include "Image.h"
#include "Node.h"
#include "BRIC.h"
#include "Renderer.h"
#include "TreeComparator.h"
#include "PriorityScorer.cpp"
#include "ScoreComparator.h"
#include "LayoutMakerFacade.h"
/*#include "Scorer2004.cpp"
#include "Scorer2008.cpp"
#include "Scorer2012.cpp"*/

using namespace std;

LayoutMakerFacade * facade;
vector<IplImage*>* solutions;
Renderer* renderer;

void print_images(vector<Image*> *images) {
    for(int i = 0; i < images->size(); i++) {
        printf("%d %d %.2f %d\n", images->at(i)->get_width(), images->at(i)->get_height(), images->at(i)->get_relative_area(), images->at(i)->get_image());
    }
}

char show_image(IplImage* image) {
    cvShowImage("", image);
    cvMoveWindow("", 0, 0);
    char c = cvWaitKey(0);
    cvDestroyWindow("");
    return c;
}
void load_images(string file_name) {
    ifstream f(file_name.c_str(), ios::in);
    if(f) {
        string image_name;
        int priority;
        while(f >> priority >> image_name) {
            string path = image_name;
            IplImage *img = cvLoadImage(path.c_str());
            facade->addImage(new Image(img->width, img->height, priority, img, path));
        }
        f.close();
    }
}

void init() {
    solutions = new vector<IplImage*>();
    facade = new LayoutMakerFacade(800,600,0,5,10);
    renderer = new Renderer(800, 600, 0, 5);
}
void run_debug(string filenames) {
    load_images(filenames);
    vector<IplImage*> rendered;
    rendered.push_back(renderer->text_draw_tree(facade->renderSolution()));
//    show_image(rendered.at(rendered.size()-1));
}

int main(int argc, char** argv) {
    if(argc < 2){
        cout << endl << "insuficient number of arguments";
        return 1;
    }
    string filename(argv[1]);
    init();
    run_debug(filename);
    return 0;
}

